﻿#include "UDPDlg.h"
#include <QTableWidgetItem>
#include "Note.h"
#include "Application.h"

UDPDlg::UDPDlg(QWidget *parent)
	: BaseDlg(parent,"UDP通道")
{
	ui.setupUi(&content_view);
	this->base_ui.content->addWidget(&content_view);

	ui.comboBox_RecvDataName->clear();
	ui.comboBox_SendDataName->clear();
	for(auto& item : Application::Instance().cfg.DataTypes())
	{
		ui.comboBox_RecvDataName->addItem(item.second.c_str(), item.first);
		ui.comboBox_SendDataName->addItem(item.second.c_str(), item.first);
	}

	/*QStringList header;
	header << "数据类型" << "地址" << "端口" << "";
	ui.tableWidget->setHorizontalHeaderLabels(header);
	ui.tableWidget->setColumnCount(col_cnt);*/
	/*connect(ui.pushButton_Modify, SIGNAL(clicked()), this, SLOT(slotModify()));
	connect(ui.pushButton_Add, SIGNAL(clicked()), this, SLOT(slotAdd()));
	connect(ui.pushButton_Ok, SIGNAL(clicked()), this, SLOT(slotApply()));*/
}

UDPDlg::~UDPDlg()
{
}

bool UDPDlg::get_info(ChannelCreateInfo& info)
{
	info.type |= CHANNEL_PROTO_TYPE_UDP;
	if(ui.lineEdit_ChannName->text().isEmpty())
	{
		Note nt{"通道名称不能为空！"};
		nt.showDialog();
		return false;
	}
	strcpy(info.title, ui.lineEdit_ChannName->text().toStdString().c_str());
	if(!info.input_address.init_client(static_cast<USHORT>(ui.spinBox_LocalPort->value())))
	{
		Note nt{ "端口号异常！" };
		nt.showDialog();
		return false;
	}
	return true;
}
//
//void UDPDlg::slotModify()
//{
//	int row = ui.tableWidget->currentRow();
//	if (row >= 0 && row <ui.tableWidget->rowCount())
//	{
//		ui.tableWidget->item(row, col_dataType)->setText(ui.comboBox_SendDataName->currentText());
//		ui.tableWidget->item(row, col_IP)->setText(ui.comboBox_PeerIP->currentText());
//		ui.tableWidget->item(row, col_Port)->setText(ui.spinBox_PeerPort->text());
//	}
//}
//
//void UDPDlg::slotAdd()
//{
//	int row = ui.tableWidget->rowCount();
//	ui.tableWidget->insertRow(row);
//
//	ui.tableWidget->setItem(row, col_dataType, new QTableWidgetItem(ui.comboBox_SendDataName->currentText()));
//	ui.tableWidget->setItem(row, col_IP, new QTableWidgetItem(ui.comboBox_PeerIP->currentText()));
//	ui.tableWidget->setItem(row, col_Port, new QTableWidgetItem(ui.spinBox_PeerPort->text()));
//	ui.tableWidget->setItem(row, col_Delete, new QTableWidgetItem(""));
//}
//
//void UDPDlg::slotApply()
//{
//	ConnInfo info;
//
//	info.type = ConnType_UDP;
//	info.chanName = ui.lineEdit_ChannName->text();
//	info.localPort = ui.spinBox_LocalPort->text().toInt();
//	info.recvDataName = ui.comboBox_RecvDataName->currentText();
//
//	for (int i=0; i<ui.tableWidget->rowCount(); ++i)
//	{
//		UDPSendCgf stUDPSendCgf;
//		stUDPSendCgf.sendDataName = ui.tableWidget->item(i, col_dataType)->text();
//		stUDPSendCgf.peerIP = ui.tableWidget->item(i, col_IP)->text();
//		stUDPSendCgf.peerPort = ui.tableWidget->item(i, col_Port)->text().toUInt();
//		info.lstUDPSendCgf.push_back(stUDPSendCgf);
//	}
//
//	if ((m_stConnInfo.localPort != info.localPort) && !m_bNewConnInfo)
//	{
//		emit sigDisConnect(m_stConnInfo, info);
//	}
//
//	emit sigConnInfo(info, m_stConnInfo, m_bNewConnInfo);
//	m_bNewConnInfo = false;
//	m_stConnInfo = info;
//
//	this->hide();
//}
